package com.thz.houserental.infrastructure.web.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.FileStorageService;
import com.thz.houserental.infrastructure.auth.AuthUtil;
import com.thz.houserental.infrastructure.web.service.impl.FileDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/file")
@Tag(name = "文件系统")
public class SysFileController {
    
    private final FileStorageService fileStorageService;
    private final FileDetailService fileDetailService;

    public SysFileController(FileStorageService fileStorageService, FileDetailService fileDetailService) {
        this.fileStorageService = fileStorageService;
        this.fileDetailService = fileDetailService;
    }

    /**
     * 上传文件到指定存储平台，成功返回文件信息
     */
    @PostMapping("/upload")
    @Operation(summary = "上传文件到指定存储平台，成功返回文件信息")
    public FileInfo uploadPlatform(@RequestParam("uploadFile") MultipartFile file) {
        String currentUserId = AuthUtil.getCurrentUserId();
        String datePath = DateUtil.format(DateTime.now(), "yyyy-MM-dd");
        String path = currentUserId + "/" + datePath + "/";
        
        FileInfo fileInfo =  fileStorageService.of(file)
                .setPlatform("tencent-cos-1")
                .setPath(path)
                .upload();
        
        return fileInfo;
    }
    
    
}
